/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.chl.mq._06_properties;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author 陈宏亮
 */
public class EmitWork {

    private final static String EXCHANGE_NAME = "chl";
    private final static Random r = new Random();

    public static void main(String[] args) throws Exception {
        //创建连接和频道  
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.2.221");
        factory.setUsername("guest");
        factory.setPassword("guest");
        try (Connection connection = factory.newConnection()) {
            Channel channel = connection.createChannel();
            for (int i = 0; i < 20; i++) {
                String message;
                if (r.nextBoolean()) {
                    message = new Date().toLocaleString() + "今天天气很好耶，是个工作的好日子。工作内容是：写代码。";
                    // 往转发器上发送消息  
                    channel.basicPublish(EXCHANGE_NAME, "chl.work", null, message.getBytes());
                } else {
                    message = new Date().toLocaleString() + "今天天气很好耶，是个学习的好日子。学习内容是：大数据。";
                    // 往转发器上发送消息  
                    channel.basicPublish(EXCHANGE_NAME, "chl.stady", null, message.getBytes());
                }
                System.out.println(" 发送日志：'" + message + "'");
            }
            channel.close();
        }
    }
}
